import axios from 'axios';

// 获取所有订单
export const getOrderList = async () => {
    try {
        const response = await axios.get(`http://localhost:8080/Orders`);
        return response.data;
    } catch (error) {
        console.error('Error getting orders:', error.response ? error.response.data : error.message);
        throw error;
    }
};


// 通过用户ID获取用户订单
export const getOrdersByUserId = async (userId) => {
    try {
        const response = await axios.get(`http://localhost:8080/Orders/getOrderByUid/${userId}`);
        return response.data;
    } catch (error) {
        console.error('Error getting orders:', error.response ? error.response.data : error.message);
        throw error;
    }
};

//增加订单
export const addOrder = async (order) => {
    try {
        const response = await axios.post(`http://localhost:8080/Orders`, order, {
            headers: {
                'Content-Type': 'application/json'
            }
        });
        return response.data;
    } catch (error) {
        console.error('Error creating order:', error.response ? error.response.data : error.message);
        throw error;
    }
};

//更新订单
export const updateOrder = async (order) => {
    try {
        const response = await axios.put(`http://localhost:8080/Orders`, order);
        return response.data;
    } catch (error) {
        console.error('Error creating order:', error.response ? error.response.data : error.message);
        throw error;
    }
};

//删除订单
export const deleteOrder = async (id) => {
    try {
        const response = await axios.delete(`http://localhost:8080/Orders/${id}`);
        return response.data;
    } catch (error) {
        console.error('Error creating order:', error.response ? error.response.data : error.message);
        throw error;
    }
};